package com.ioc.barry.lsn_8_pathmeasure;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class LoadView extends View {

    private float currentValue = 0;     // 用于纪录当前的位置,取值范围[0,1]映射Path的整个长度

    private float[] pos;                // 当前点的实际位置
    private float[] tan;                // 当前点的tangent值,用于计算图片所需旋转的角度
    private Bitmap mBitmap;             // 箭头图片
    private Matrix mMatrix;             // 矩阵,用于对图片进行一些操作
    private Paint mDeafultPaint;
    private int mViewWidth;
    private int mViewHeight;
    private Paint mPaint;


    public LoadView(Context context) {
        this(context,null);
    }

    public LoadView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LoadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr,0);
    }

    public LoadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(this.getContext());
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mViewWidth = w;
        mViewHeight = h;
    }


    private void init(Context context) {
        pos = new float[2];
        tan = new float[2];
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;       // 缩放图片
        mBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.arrow, options);
        mMatrix = new Matrix();

        mDeafultPaint = new Paint();
        mDeafultPaint.setColor(Color.RED);
        mDeafultPaint.setStrokeWidth(5);
        mDeafultPaint.setStyle(Paint.Style.STROKE);

        mPaint = new Paint();
        mPaint.setColor(Color.DKGRAY);
        mPaint.setStrokeWidth(2);
        mPaint.setStyle(Paint.Style.STROKE);
    }


    @Override
    protected void onDraw(Canvas canvas) {


        //100  5000
        currentValue += 0.005;
        if(currentValue >=1){
            currentValue = 0;
        }


        canvas.drawColor(Color.WHITE);

        canvas.translate(mViewWidth / 2,mViewHeight / 2);

        Path path = new Path();

        //Path.Direction.CW顺时针方向
        path.addCircle(0,0,200,Path.Direction.CCW);

        PathMeasure measure = new PathMeasure(path,false);

        measure.getPosTan(measure.getLength() * currentValue,pos,tan);
        //Math.atan2()函数返回点(x,y)和原点(0,0)之间直线的倾斜角,圆心即圆点
        float degress = (float) (Math.atan2(tan[1],tan[0]) * 180 / Math.PI);

        mMatrix.reset();
        mMatrix.postRotate(degress,mBitmap.getWidth() / 2,mBitmap.getHeight() / 2);
        mMatrix.postTranslate(pos[0] - mBitmap.getWidth() / 2,pos[1] - mBitmap.getHeight() / 2);



        canvas.drawPath(path,mDeafultPaint);
        canvas.drawBitmap(mBitmap,mMatrix,mPaint);


        invalidate();
    }
}
